package com.unytech.wisdomcommunity.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.unytech.wisdomcommunity.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;

public class EmptyActivity extends AppCompatActivity {

    private Button mBtn;
    private TextView tv_1,tv_2,tv_cel;
    private ImageView pictureIv;

    public static final int TAKE_PHOTO = 1;
    public static final int CROP_PHOTO = 2;
    public static final int GALLERY = 3;

    private Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_empty);

        pictureIv = ((ImageView) findViewById(R.id.picture));

        mBtn = ((Button) findViewById(R.id.btn));
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow(v);
            }
        });
    }

    private void showPopupWindow(View v) {
        View pupView = LayoutInflater.from(this).inflate(R.layout.popup_window_layout, null);

        tv_1 = ((TextView) pupView.findViewById(R.id.pop_tv_1));
        tv_2 = ((TextView) pupView.findViewById(R.id.pop_tv_2));
        tv_cel = ((TextView) pupView.findViewById(R.id.pop_tv_cel));

        //点击打开 手机相册
        tv_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            // 创建File 对象，用于存储选择的照片
           /*     File imageFile = new File(Environment.
                        getExternalStorageDirectory(), "output_image.jpg");
                try {
                    if (imageFile.exists()){
                        imageFile.delete();
                    }
                    imageFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                imageUri = Uri.fromFile(imageFile);
                Toast.makeText(getApplicationContext(),"点击了1",Toast.LENGTH_SHORT).show();
*//*
                Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent,GALLERY);*//*
                Intent intent = new Intent("android.intent.action.GET_CONTENT");
                intent.setType("image*//*");
                intent.putExtra("crop", true);
                intent.putExtra("scale", true);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, CROP_PHOTO);*/
                File outputImage = new File(Environment.
                        getExternalStorageDirectory(), "output_image.jpg");
                try {
                    if (outputImage.exists()) {
                        outputImage.delete();
                    }
                    outputImage.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                imageUri = Uri.fromFile(outputImage);
                //Intent intent = new Intent("android.intent.action.GET_CONTENT");
                Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                /*intent.setType("image*//*");
                intent.putExtra("crop", true);
                intent.putExtra("scale", true);*/
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, GALLERY);
            }
        });
        //点击开启  手机的摄像头
        tv_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar =Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);
                int hour_of_day = calendar.get(Calendar.HOUR_OF_DAY);
                int minute = calendar.get(Calendar.MINUTE);
                int second = calendar.get(Calendar.SECOND);

                File imageFile = new File(Environment.getExternalStorageDirectory(),
                        year+month+day_of_month+hour_of_day+minute+second+".jpg");

                try {
                    if (imageFile.exists()){
                        imageFile.delete();
                    }
                    imageFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                imageUri = Uri.fromFile(imageFile);

                Toast.makeText(getApplicationContext(),"点击了拍照",Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                startActivityForResult(intent,TAKE_PHOTO);//点击开启手机相机程序
            }
        });

        final PopupWindow popupWindow = new PopupWindow(pupView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        /*final PopupWindow popupWindow = new PopupWindow(pupView,
                100, 80, true);*/

        popupWindow.setTouchable(true);

        //把事件拦截下来
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));

        // 设置好参数之后再show
        //popupWindow.showAsDropDown(v);
        popupWindow.showAtLocation(v, Gravity.BOTTOM,0,0);

        tv_cel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case TAKE_PHOTO:
                    if (resultCode == RESULT_OK){
                        Intent cropIntent = new Intent("com.android.camera.action.CROP");

                        cropIntent.setDataAndType(imageUri, "image/*");
                        cropIntent.putExtra("scale", true);
                        cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

                        startActivityForResult(cropIntent,CROP_PHOTO);
                    }
                break;
            case CROP_PHOTO:
                if (resultCode == RESULT_OK) {
                    try {
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                        pictureIv.setImageBitmap(bitmap); // 将裁剪后的照片显示出来
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case GALLERY:
                if (resultCode == RESULT_OK){
                    Intent cropIntent = new Intent("com.android.camera.action.CROP");

                    Toast.makeText(getApplicationContext(),"相册打开了",Toast.LENGTH_SHORT).show();

                    cropIntent.setDataAndType(imageUri, "outimage/*");
                    cropIntent.putExtra("scale", true);
                    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

                    startActivityForResult(cropIntent,CROP_PHOTO);
                }
                break;
            default:
                break;
        }
    }
}